/*
Birth order models for NLSY
Last changed by SAO: 191008
*/


clear

set more off

use "D:\Data\ProjData\SiblingOrder\OtherSamples\NLSY79\NLSY79A.dta", clear
drop _merge

***Generate birth order indicator
rename BIRTHORDER BIRTHORDER3
bysort MOTHER_ID: egen BIRTHORDER = rank(BYEAR), track

***Restrict to 2-5 sibling families
keep if inrange(OFFSPRING, 2, 5)

***Drop families with twins
gen occ = 1
bysort MOTHER_ID BYEAR: egen nocc = sum(occ)
tab nocc
keep if nocc==1
drop occ nocc

rename MOTHER_ID R0000100

merge m:1 R0000100 using "D:\Data\ProjData\SiblingOrder\OtherSamples\NLSY79\Mothers.dta", keepusing(R0000600 R8497000)

drop if _merge==2
drop _merge

rename R0000100 MOTHER_ID

gen MBYEAR = 1979-R0000600
gen MEDUC = R8497000 if inrange(R8497000, 0, 20)

gen SEX = C0005400

***Generate birth order dummy
gen BIRTHORDER_DUM = .
replace BIRTHORDER_DUM = 0 if inrange(BIRTHORDER, 2, 11)
replace BIRTHORDER_DUM = 1 if BIRTHORDER==1

***Generate dummy for voting in 2004 and/or 2006 election
egen VOTE_ANY = rowmax(VOTE2004 VOTE2006)

***Generate family size indicator
egen FAMSIZE = rownonmiss(C0000300 C0000400 C0000500 C0000600 C0000700 C0000800 C0000810 C0000811 C0000812 C0000813)
replace FAMSIZE = FAMSIZE+1


***Save file for pooled analysis
keep MOTHER_ID VOTE2004 BIRTHORDER BIRTHORDER_DUM SEX BYEAR INTEREST INT_EFF EXT_EFF DUTY
rename MOTHER_ID famid
rename VOTE2004 turnout
rename BIRTHORDER birthorder
rename BIRTHORDER_DUM birthorder_dum
rename SEX female
rename BYEAR age
rename INTEREST polinterest
rename INT_EFF inteff
rename EXT_EFF exteff
rename DUTY civicduty
replace female = female-1
replace age = 2004-age //age in 2004
drop if age<0
gen sample = "NLSY"
qui: reghdfe turnout birthorder_dum female i.age, absorb(famid) cluster(famid)
sum turnout if e(sample)
gen turnout_average = r(mean)

save "D:\Data\ProjData\SiblingOrder\ReplicationNLSY data 191128.dta", replace

